package newcall.com.newcalldemo;

import android.content.Context;
import android.content.SharedPreferences;

import com.m800.sdk.M800SDK;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


public class LogoutTask {

    private static final String DEBUG_TAG = LogoutTask.class.getSimpleName();

    public static final String PREV_USER_DATA = LogoutTask.class.getSimpleName();
    public static final String KEY_PREV_USER_NAME = "prevUserNumber";

    private final Context application;
//    private final M800Service m800Service;

    private static Executor sExecutor = Executors.newSingleThreadExecutor();

    public LogoutTask(Context application) {
        this.application = application;
//        this.m800Service = m800Service;
    }


    public void execute() {
        // Signout
        sExecutor.execute(new Runnable() {
            @Override
            public void run() {
                M800SDK.getInstance().getManagement().signout();
                saveCurrentUserPhoneNumber();

            }
        });

        // Save current user's phone number for next login


        // Clear chat list cache
//
//        if (m800Service != null) {
//            sExecutor.execute(new Runnable() {
//                @Override
//                public void run() {
//                    m800Service.clearChatRoomList(); //because this method is synchronized so maybe blocked by other threads..
//                }
//            });
//            m800Service.clearChatMessageNotification();
//            m800Service.clearAddContactRequestNotification();
//            m800Service.clearNewContactNotification();
//        }

//        final Intent intent = new Intent(application, IntroductionActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//        application.startActivity(intent);
    }

    private void saveCurrentUserPhoneNumber() {
        String userName = M800SDK.getInstance().getUsername();
        SharedPreferences sharedPreferences = application.getSharedPreferences(
                PREV_USER_DATA, Context.MODE_PRIVATE);
        sharedPreferences.edit().putString(KEY_PREV_USER_NAME, userName).apply();
    }
}
